namespace EyeSoft.Testing.IO
{
	using System;
	using System.Collections.Generic;
	using System.IO;
	using System.Linq;
	using System.Text;

	using EyeSoft.IO;

	public class MemoryStorage
		: IStorage
	{
		private readonly IList<IFileInfo> fileCollection;

		public MemoryStorage()
		{
			fileCollection = new List<IFileInfo>();
		}

		public MemoryStorage(IEnumerable<IFileInfo> fileCollection)
		{
			this.fileCollection = new List<IFileInfo>(fileCollection);
		}

		public IFileInfo File(string path)
		{
			Enforce.Argument(() => path);

			var fileInfo =
				fileCollection
					.FirstOrDefault(file => file.FileName == path);

			Ensure
				.That(fileInfo)
				.WithException(new FileNotFoundException(string.Format("The file \"{0}\" does not exist.", path), path))
				.Is.Not.Null();

			return
				fileInfo;
		}

		public IDirectoryInfo Directory(string path)
		{
			throw new NotImplementedException();
		}

		public Stream OpenRead(string path)
		{
			throw new NotImplementedException();
		}

		public IEnumerable<IFileInfo> GetFiles(string path, string pattern)
		{
			return
				fileCollection
					.Where(d => d.FileName == pattern)
					.AsEnumerable();
		}

		public void WriteAllBytes(string path, byte[] bytes)
		{
			fileCollection
				.Add(new MemoryFileInfo(path, bytes));
		}

		public void WriteAllText(string path, string contents, bool b)
		{
			WriteAllBytes(path, Encoding.Default.GetBytes(contents));
		}

		public Stream OpenWrite(string name)
		{
			throw new NotSupportedException();
		}

		public string ReadAllText(string path)
		{
			throw new NotSupportedException();
		}

		public byte[] ReadAllBytes(string path)
		{
			throw new NotSupportedException();
		}

		public bool Exists(string path)
		{
			throw new NotSupportedException();
		}

		public void Delete(string path)
		{
			throw new NotSupportedException();
		}

		public string GetFileNameWithoutExtension(string path)
		{
			throw new NotImplementedException();
		}

		public void Write(string path, Stream stream)
		{
			throw new NotSupportedException();
		}
	}
}